local util = require "util"
local conf = require "conf"
local require = require
local setmetatable = setmetatable
local error = error
local table = table
local ngx = ngx
local os = os
local string = string
local tonumber = tonumber
local pairs = pairs
package.cpath = "/usr/nginx/?.so;;"
local json = require 'cjson'
module(...)
_VERSION = '0.1'

function  create(user, args)
    local db = conf.get_mysql()
    local sql = "INSERT INTO `tower` SET"

    if args.id then
        sql = sql .. " id = " .. args.id .. ","
    end
    
    if args.spoint_id then
        sql = sql .. " spoint_id = " .. args.spoint_id .. ","
    end

    sql = sql .. " create_time = " .. os.time() .. ","

    sql = sql .. " update_time = " .. os.time() .. ","
    sql = sql:sub(1, -2)
    ngx.log(ngx.ERR, sql)
    local res = db:query(sql)
    return res
end

function registerController(user, args)
    if not args.id or not args.spoint_id then
        ngx.print(json.encode({
            err = 1,
            msg = '参数不对'
        }))
        ngx.exit(200)
    end

    local db = conf.get_mysql()
    local red = conf.get_redis()
    local res = search(user, args)
    if res and res[1] then
        res = update(user, args)
    else
        res = create(user, args)
    end
    local pressure_alt = red:get("pressure_alt_" ..args.id)
    -- local res = create(user, args)
    if not res then
        ngx.print(json.encode({
            err = 1,
            msg = '注册失败'
        }))
        ngx.exit(200)
    else
        ngx.print(json.encode({
            err = 0,
            msg = '注册成功',
            pressure_alt = pressure_alt
        }))
        ngx.exit(200)
    end 
end

function update(user, args)
    local db = conf.get_mysql()
    local sql = "UPDATE `tower` SET "

    if args.spoint_id then
        sql = sql .. " spoint_id = " .. args.spoint_id .. ","
    end

    sql = sql .. " update_time = " .. os.time() .. ","
    ngx.log(ngx.ERR, sql)
    local res = db:query(sql:sub(1, -2) .. " WHERE id = " .. args.id )
    return res
end

function search(user, args)
    local page_no, page_size, id, name, aid = tonumber(args.page_no), tonumber(args.page_size), args.id, args.name, args.aid
    local db = conf.get_mysql()
    local plane = require "plane"
    local now  = os.time()

    local sql = "SELECT `tower`.id, `tower`.spoint_id, `spoint`.lng, `spoint`.lat, `spoint`.name from `tower` left join `spoint` on `spoint`.id = `tower`.spoint_id WHERE 1=1";

    if id then
        sql = sql .. " AND `tower`.id = " .. id
    end

    ngx.log(ngx.ERR, sql)
    local count = #(db:query(sql) or {})

    if page_no and page_size then
        sql = sql ..  " LIMIT " .. (page_no * page_size) .. "," .. page_size
    end
    local res = db:query(sql)
    ngx.log(ngx.ERR, json.encode(res))
    return res,count
end

function searchController(user, args)
    local res, count = search(user, args)
    if not res then
        ngx.print(json.encode({
            err = 1,
            msg = '未能查询到'
        }))
        ngx.exit(200)
    else
        ngx.print(json.encode({
            err = 0,
            msg = res,
            count = count
        }))
        ngx.exit(200)
    end
end

function default(args)
    local user = conf.parse_token(args, "spoint")
    if args.action == 'register' then
        registerController(user, args)
    elseif args.action == 'search' then
        searchController(user, args)
    end
end

local class_mt = {
    __newindex = function (table, key, val)
        error('attempt to write to undeclared variable "' .. key .. '"')
    end
}
setmetatable(_M, class_mt)
